<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="record.rule_id ? $t('编辑') : $t('添加')" append-to-body :v-if="dialogVisible" :visible.sync="dialogVisible" width="530px" top="5vh" @close='close'>
    <div class="dialogContentBoxMaxHeight90">
      <el-form ref="form" :inline="true" :model="record" label-width="150px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('协议名称')" class="iec104Form" prop="proto">
          <span style="color:#999;">{{ rule[record.proto] }}</span>
        </el-form-item>
        <el-form-item :label="$t('类型')" prop="operate_type">
          <el-radio-group v-model="record.operate_type" :disabled='record.rule_id ? true : false' @change="operateTypeChange">
            <el-radio label="0" class="deepfilts">{{$t('深度过滤')}}</el-radio>
            <el-radio label="1" class="operate">{{$t('关联操作')}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('动作')" class="iec104Form" prop="action">
          <el-select v-model="record.action" ref="action" class="iec104Length" size='small' filterable>
            <el-option v-for="(value, key) in actionList" :key="key" :label="$t(value)" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <div v-if="record.operate_type === '0'">
          <el-form-item :label="$t('数据类型')" class="iec104Form" prop="type">
            <el-select v-model="record.type" ref="type" class="iec104Length" size='small' filterable @change="typeChange">
              <el-option v-for="(value, key) in typeList" :key="key" :label="$t(value)" :value="key">
              </el-option>
            </el-select>
          </el-form-item>
          <!-- --------------------------------I帧--------------------------------- -->
          <div v-if="record.type == '0'">
            <el-form-item :label="$t('功能码')" class="iec104Form" prop="func">
              <el-select v-model="record.func" ref="func" class="iec104Length" size='small' filterable @change="funcChange">
                <el-option v-for="item in funcList" :key="item.value" :label="$t(item.label)" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('匹配范围')" class="iec104Form" prop="action">
              <el-radio-group v-model="record.accept_range">
                <el-radio label="all" class="all">{{$t('所有')}}</el-radio>
                <el-radio label="single" class="single">{{$t('单个')}}</el-radio>
              </el-radio-group>
            </el-form-item>
            <div v-if="record.accept_range === 'single'">
              <el-form-item :label="$t('传送原因')" class="iec104Form" prop="transfer_reason" key="transfer_reason">
                <el-input v-emoji v-model="record.transfer_reason" ref="ranson" class="iec104Length" size='small'></el-input>
              </el-form-item>
              <el-form-item :label="$t('信息对象起始地址')" class="iec104Form" prop="information_object_address_start" key="information_object_address_start">
                <el-input v-emoji v-model="record.information_object_address_start" ref="object" class="iec104Length" size='small'></el-input>
              </el-form-item>
              <el-form-item :label="$t('信息对象结束地址')" class="iec104Form" prop="information_object_address_end" key="information_object_address_end">
                <el-input v-emoji v-model="record.information_object_address_end" ref="information" class="iec104Length" size='small'></el-input>
              </el-form-item>
              <el-form-item :label="$t('公共起始地址')" class="iec104Form" prop="common_address_start" key="common_address_start">
                <el-input v-emoji v-model="record.common_address_start" ref="common" class="iec104Length" size='small'></el-input>
              </el-form-item>
              <el-form-item :label="$t('公共结束地址')" class="iec104Form" prop="common_address_end" key="common_address_end">
                <el-input v-emoji v-model="record.common_address_end" ref="ends" class="iec104Length" size='small'></el-input>
              </el-form-item>
              <el-form-item :label="$t('寄存器值范围')" prop="register_value_range_select" key="register_value_range_select" v-if="showSelectList.includes(record.func)" style="margin-right:0">
                <el-select v-model="record.register_value_range_select" ref="rangeSelect" class="iec104Length" @change="registerRangeChange" size='small' filterable>
                  <el-option v-for="(value, key) in registerValueRangeSelectList" :key="key" :label="$t(value)" :value="key">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item :label="$t('寄存器值范围')" prop="register_value_range_start" v-if="!showSelectList.includes(record.func)" key="register_value_range_start" style="margin-right:0">
                <el-input v-emoji v-model="record.register_value_range_start" ref="registerStart" style="width:117px" @change="registerRangeChange" size='small'></el-input>
              </el-form-item>
              <el-form-item label="" v-if="!showSelectList.includes(record.func)" style="margin-right:0">~</el-form-item>
              <el-form-item label-width="0" v-if="!showSelectList.includes(record.func)" prop="register_value_range_end" key="register_value_range_end">
                <el-input v-emoji v-model="record.register_value_range_end" ref="registerEnd" @change="registerRangeChange" style="width:117px" size='small'></el-input>
              </el-form-item>
            </div>
          </div>
        </div>
        <div v-if="record.operate_type === '1'">
          <el-form-item :label="$t('写操作功能码')" class="iec104Form" prop="relate_func">
            <el-select v-model="record.relate_func" ref="relateFunc" class="iec104Length" size='small' filterable>
              <el-option v-for="item in funcList" :key="item.value" :label="$t(item.label)" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('写操作传送原因')" class="iec104Form" prop="relate_transfer_reason" key="relate_transfer_reason">
            <el-input v-emoji v-model="record.relate_transfer_reason" ref="reasons" class="iec104Length" size='small'></el-input>
          </el-form-item>
          <el-form-item :label="$t('写操作信息体地址')" class="iec104Form" prop="relate_information_object_address" key="relate_information_object_address">
            <el-input v-emoji v-model="record.relate_information_object_address" ref="address" class="iec104Length" size='small'></el-input>
          </el-form-item>
          <el-form-item :label="$t('写操作公共地址')" class="iec104Form" prop="relate_common_address" key="relate_common_address">
            <el-input v-emoji v-model="record.relate_common_address" ref="common" class="iec104Length" size='small'></el-input>
          </el-form-item>
          <el-form-item :label="$t('关联写操作功能码')" class="iec104Form" prop="be_relate_func">
            <el-select v-model="record.be_relate_func" class="iec104Length" size='small' ref="beRelateFunc" filterable @change="relateFuncChange">
              <el-option v-for="item in funcList" :key="item.value" :label="$t(item.label)" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('关联写操作传送原因')" class="iec104Form" prop="be_relate_transfer_reason" key="be_relate_transfer_reason">
            <el-input v-emoji v-model="record.be_relate_transfer_reason" ref="transfer" class="iec104Length" size='small'></el-input>
          </el-form-item>
          <el-form-item :label="$t('关联写操作信息体地址')" class="iec104Form" prop="be_relate_information_object_address" key="be_relate_information_object_address">
            <el-input v-emoji v-model="record.be_relate_information_object_address" ref="object" class="iec104Length" size='small'></el-input>
          </el-form-item>
          <el-form-item :label="$t('关联写操作公共地址')" class="iec104Form" prop="be_relate_common_address" key="be_relate_common_address">
            <el-input v-emoji v-model="record.be_relate_common_address" ref="commonAddress" class="iec104Length" size='small'></el-input>
          </el-form-item>
          <el-form-item :label="$t('关联信息体元素值范围')" prop="register_value_range_select_between" key="register_value_range_select_between" v-if="showSelectList.includes(record.be_relate_func)" style="margin-right:0">
            <el-select v-model="record.register_value_range_select_between" ref="selectBetween" class="iec104Length" @change="registerRangeBetweenChange" size='small' filterable>
              <el-option v-for="(value, key) in registerValueRangeSelectList" :key="key" :label="$t(value)" :value="key">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('关联信息体元素值范围')" prop="register_value_range_start_between" v-if="!showSelectList.includes(record.be_relate_func)" key="register_value_range_start_between" style="margin-right:0">
            <el-input v-emoji v-model="record.register_value_range_start_between" ref="start" style="width:117px" @change="registerRangeBetweenChange" size='small'></el-input>
          </el-form-item>
          <el-form-item label="" v-if="!showSelectList.includes(record.be_relate_func)" style="margin-right:0">~</el-form-item>
          <el-form-item label-width="0" v-if="!showSelectList.includes(record.be_relate_func)" prop="register_value_range_end_between" key="register_value_range_end_between">
            <el-input v-emoji v-model="record.register_value_range_end_between" ref="end" @change="registerRangeBetweenChange" style="width:117px" size='small'></el-input>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="innerAddSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="innerAddClose">{{$t('取 消')}}</el-button>
    </span>

  </el-dialog>
</template>
<script>
import ruleArray from '../../ruleArray'
import { selectClass, inputClass } from '@/utils/input-class.js'
export default {
  name: 'Iec104Dialog',
  data () {
    var validateInformation = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 0 || Number(value) > 65535) {
        callback(new Error(this.$t('输入范围0~65535')))
      } else if (this.record.information_object_address_start && this.record.information_object_address_end && (Number(this.record.information_object_address_start) > Number(this.record.information_object_address_end))) {
        callback(new Error(this.$t('起始地址必须小于结束地址')))
      } else {
        this.chearRule('information_object_address_start')
        this.chearRule('information_object_address_end')
        callback()
      }
    }
    var validateCommon = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 0 || Number(value) > 65535) {
        callback(new Error(this.$t('输入范围0~65535')))
      } else if (this.record.common_address_start && this.record.common_address_end && (Number(this.record.common_address_start) > Number(this.record.common_address_end))) {
        callback(new Error(this.$t('起始地址必须小于结束地址')))
      } else {
        this.chearRule('common_address_start')
        this.chearRule('common_address_end')
        callback()
      }
    }
    var validateRegisterValueRangeStart = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (['48', '61'].includes(this.record.func) && (Number(value) < 0 || Number(value) > 2)) {
        callback(new Error(this.$t('输入范围0.0-2.0')))
      } else if (['49', '62'].includes(this.record.func) && (Number(value) < 0 || Number(value) > 65535)) {
        callback(new Error(this.$t('输入范围0~65535')))
      } else if (['49', '62'].includes(this.record.func) && !this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (['51', '64'].includes(this.record.func) && (!this.isInteger(value) || Number(value) < 0)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (this.record.register_value_range_start && this.record.register_value_range_end && (Number(this.record.register_value_range_start) > Number(this.record.register_value_range_end))) {
        callback(new Error(this.$t('不能大于最大值')))
      } else {
        callback()
      }
    }
    var validateRegisterValueRangeEnd = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (['48', '61'].includes(this.record.func) && (Number(value) < 0 || Number(value) > 2)) {
        callback(new Error(this.$t('输入范围0.0-2.0')))
      } else if (['49', '62'].includes(this.record.func) && (Number(value) < 0 || Number(value) > 65535)) {
        callback(new Error(this.$t('输入范围0~65535')))
      } else if (['49', '62'].includes(this.record.func) && !this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (['51', '64'].includes(this.record.func) && (!this.isInteger(value) || Number(value) < 0)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (this.record.register_value_range_start && this.record.register_value_range_end && (Number(this.record.register_value_range_start) > Number(this.record.register_value_range_end))) {
        callback(new Error(this.$t('不能小于最小值')))
      } else {
        callback()
      }
    }
    var validateRelateTransferReason = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if ((Number(value) < 0 || Number(value) > 256)) {
        callback(new Error(this.$t('输入范围0~256')))
      } else {
        callback()
      }
    }
    var validateRelateInformationObjectAddress = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if ((Number(value) < 0 || Number(value) > 16777215)) {
        callback(new Error(this.$t('输入范围0~16777215')))
      } else {
        callback()
      }
    }
    var validateRelateCommonAddress = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if ((Number(value) < 0 || Number(value) > 65535)) {
        callback(new Error(this.$t('输入范围0~65535')))
      } else {
        callback()
      }
    }
    var validateRegisterStart = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (['48', '61'].includes(this.record.be_relate_func) && (Number(value) < 0 || Number(value) > 2)) {
        callback(new Error(this.$t('输入范围0.0-2.0')))
      } else if (['49', '62'].includes(this.record.be_relate_func) && (Number(value) < 0 || Number(value) > 65535)) {
        callback(new Error(this.$t('输入范围0~65535')))
      } else if (['49', '62'].includes(this.record.be_relate_func) && !this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (['51', '64'].includes(this.record.be_relate_func) && (!this.isInteger(value) || Number(value) < 0)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (this.record.register_value_range_start_between && this.record.register_value_range_end_between && (Number(this.record.register_value_range_start_between) > Number(this.record.register_value_range_end_between))) {
        callback(new Error(this.$t('不能大于最大值')))
      } else {
        callback()
      }
    }
    var validateRegisterEnd = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (['48', '61'].includes(this.record.be_relate_func) && (Number(value) < 0 || Number(value) > 2)) {
        callback(new Error(this.$t('输入范围0.0-2.0')))
      } else if (['49', '62'].includes(this.record.be_relate_func) && (Number(value) < 0 || Number(value) > 65535)) {
        callback(new Error(this.$t('输入范围0~65535')))
      } else if (['49', '62'].includes(this.record.be_relate_func) && !this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (['51', '64'].includes(this.record.be_relate_func) && (!this.isInteger(value) || Number(value) < 0)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (this.record.register_value_range_start_between && this.record.register_value_range_end_between && (Number(this.record.register_value_range_start_between) > Number(this.record.register_value_range_end_between))) {
        callback(new Error(this.$t('不能小于最小值')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        accept_range: 'all',
        action: 'accept',
        common_address_end: '0',
        common_address_start: '0',
        func: '0',
        information_object_address_end: '0',
        information_object_address_start: '0',
        match_direction: '-',
        proto: 'iec104',
        register_value_range: '0-0', // start - end
        rule_id: null,
        transfer_reason: '0',
        type: '0',
        // ------
        register_value_range_end: '',
        register_value_range_select: '0',
        register_value_range_start: '',
        operate_type: '0',
        relate_func: '45',
        relate_transfer_reason: '',
        relate_information_object_address: '',
        relate_common_address: '',
        be_relate_func: '45',
        be_relate_transfer_reason: '',
        be_relate_information_object_address: '',
        be_relate_common_address: '',
        register_value_range_select_between: '0',
        register_value_range_start_between: '',
        register_value_range_end_between: '',
        be_relate_register_value_range: ''
      },
      recordCopy: {
        accept_range: 'all',
        action: 'accept',
        common_address_end: '0',
        common_address_start: '0',
        func: '0',
        information_object_address_end: '0',
        information_object_address_start: '0',
        match_direction: '-',
        proto: 'iec104',
        register_value_range: '0-0', // start - end
        rule_id: null,
        transfer_reason: '0',
        type: '0',
        // ------
        register_value_range_end: '',
        register_value_range_select: '0',
        register_value_range_start: '',
        operate_type: '0',
        relate_func: '45',
        relate_transfer_reason: '',
        relate_information_object_address: '',
        relate_common_address: '',
        be_relate_func: '45',
        be_relate_transfer_reason: '',
        be_relate_information_object_address: '',
        be_relate_common_address: '',
        register_value_range_select_between: '0',
        register_value_range_start_between: '',
        register_value_range_end_between: '',
        be_relate_register_value_range: ''
      },
      rule: ruleArray.ruleShow,
      funcList: ruleArray.iecFuncList,
      typeList: ruleArray.iesTypeList,
      actionList: { 'accept': '允许', 'alert': '告警' },
      registerValueRangeSelectList: {
        '0': '关',
        '1': '开',
        '2': '开关'
      },
      showSelectList: ['45', '46', '47', '58', '59', '60'],
      rules: {
        transfer_reason: [{ required: false, validator: validateRelateTransferReason, trigger: 'blur' }],
        information_object_address_start: [{ required: false, validator: validateInformation, trigger: 'blur' }],
        information_object_address_end: [{ required: false, validator: validateInformation, trigger: 'blur' }],
        common_address_start: [{ required: false, validator: validateCommon, trigger: 'blur' }],
        common_address_end: [{ required: false, validator: validateCommon, trigger: 'blur' }],
        register_value_range_start: [{ required: false, validator: validateRegisterValueRangeStart, trigger: 'blur' }],
        register_value_range_end: [{ required: false, validator: validateRegisterValueRangeEnd, trigger: 'blur' }],
        relate_transfer_reason: [{ required: false, validator: validateRelateTransferReason, trigger: 'blur' }],
        relate_information_object_address: [{ required: false, validator: validateRelateInformationObjectAddress, trigger: 'blur' }],
        relate_common_address: [{ required: false, validator: validateRelateCommonAddress, trigger: 'blur' }],
        be_relate_transfer_reason: [{ required: false, validator: validateRelateTransferReason, trigger: 'blur' }],
        be_relate_information_object_address: [{ required: false, validator: validateRelateInformationObjectAddress, trigger: 'blur' }],
        be_relate_common_address: [{ required: false, validator: validateRelateCommonAddress, trigger: 'blur' }],
        register_value_range_start_between: [{ required: false, validator: validateRegisterStart, trigger: 'blur' }],
        register_value_range_end_between: [{ required: false, validator: validateRegisterEnd, trigger: 'blur' }]
      }
    }
  },
  computed: {
    change () {
      const { accept_range, operate_type, func } = this.record
      return { accept_range, operate_type, func }
    }
  },
  watch: {
    change (val) {
      if (val.operate_type === '0') {
        if (val.accept_range === 'single') {
          this.$nextTick(() => {
            const data = ['ranson', 'object', 'information', 'common', 'ends']
            inputClass(this.$refs, data)
            const selectData = ['rangeSelect']
            selectClass(this.$refs, selectData)
          })
          if (this.showSelectList.includes(val.func)) {
            this.$nextTick(() => {
              selectClass(this.$refs, ['rangeSelect'])
            })
          } else {
            this.$nextTick(() => {
              const data = ['registerStart', 'registerEnd']
              inputClass(this.$refs, data)
            })
          }
        }
      }
      if (val.operate_type === '1') {
        this.$nextTick(() => {
          const data = ['reasons', 'address', 'common', 'transfer', 'object', 'commonAddress']
          inputClass(this.$refs, data)
          const selectData = ['relateFunc', 'beRelateFunc', 'selectBetween']
          selectClass(this.$refs, selectData)
        })
      }
    }
    // 'record.func' (val) {

    // }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const selectData = ['action', 'type', 'func']
        selectClass(this.$refs, selectData)
      })
      if (this.record.type === '0') {
        this.record.func = '45'
      }
      this.registerRangeBetweenChange()
      if (data) {
        this.record = Object.assign(this.record, data)
        this.record.register_value_range_start = this.showSelectList.includes(this.record.func) ? '' : (this.record.register_value_range.split('-')[0] || '')
        this.record.register_value_range_end = this.showSelectList.includes(this.record.func) ? '' : (this.record.register_value_range.split('-')[1] || '')
        this.record.register_value_range_select = this.showSelectList.includes(this.record.func) ? (this.record.register_value_range.split('-')[0] || '0') : '0'
      }
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          var formData = JSON.parse(JSON.stringify(this.record))
          this.$emit('addConfigRuleMap', formData)
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.$emit('childrenClose')
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    funcChange (val) {
      this.record.register_value_range_start = ''
      this.record.register_value_range_end = ''
      this.record.register_value_range_select = '0'
      this.registerRangeChange()
    },
    registerRangeChange () {
      if (this.showSelectList.includes(this.record.func)) {
        this.record.register_value_range = this.record.register_value_range_select + '-0'
      } else {
        this.record.register_value_range = this.record.register_value_range_start + '-' + this.record.register_value_range_end
      }
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    },
    operateTypeChange (data) {
      this.record.proto = data === '0' ? 'iec104' : 'iec104_connect'
    },
    relateFuncChange (val) {
      this.record.register_value_range_start_between = ''
      this.record.register_value_range_end_between = ''
      this.record.register_value_range_select_between = '0'
      this.registerRangeBetweenChange()
    },
    registerRangeBetweenChange () {
      if (this.showSelectList.includes(this.record.be_relate_func)) {
        this.record.be_relate_register_value_range = this.record.register_value_range_select_between + '-0'
      } else {
        this.record.be_relate_register_value_range = this.record.register_value_range_start_between + '-' + this.record.register_value_range_end_between
      }
    },
    typeChange (data) {
      if (data === '0') {
        this.record.func = '45'
      } else {
        this.record.func = '0'
      }
    },
    chearRule (name) {
      if (!isNaN(Number(this.record[name])) && this.isInteger(this.record[name]) && (Number(this.record[name]) >= 0 && Number(this.record[name]) <= 65535)) {
        this.$refs['form'].clearValidate([name])
      }
    }
  }
}
</script>
<style>
.iec104Length {
  width: 250px;
}
</style>
